package com.js.server.controller;

import com.js.server.pojo.Admin;
import com.js.server.pojo.AdminLoginParam;
import com.js.server.pojo.RespBean;
import com.js.server.service.IAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

/**
 * @author ：js
 * @date ：Created in 2021-03-04 16:09
 * @description: 登录接口
 * @version: 1.0
 */
@RestController
@Api(tags = "LoginController")
public class LoginController {

    @Autowired
    private IAdminService adminService;

    @ApiOperation(value = "登录之后返回token")
    @PostMapping("login")
    public RespBean login(AdminLoginParam adminLoginParam, HttpServletRequest request) {
        return adminService.login(adminLoginParam.username(), adminLoginParam.password(), request);
    }

    @ApiOperation(value = "获取当前登录用户的信息")
    @PostMapping("admin/info")
    public Admin getAdminInfo(Principal principal) {
        if (null == principal) {
            return null;
        }
        String username = principal.getName();
        Admin admin = adminService.getAdminInfoByUserName(username);
        admin.setPassword(null);
        return admin;
    }

    @ApiOperation(value = "退出登录")
    @PostMapping("logout")
    public RespBean logout() {
        //前端去处理这个请求
        return RespBean.success("注销成功!");
    }
}
